package com.lagou.server;

import com.lagou.model.Questions;
import com.lagou.model.Student;
import com.lagou.model.StudentMessage;
import com.lagou.model.UserMessage;

import java.io.IOException;
import java.util.ArrayList;

/**
 * 编程实现服务器的主功能
 */
public class ServerView {

    /**
     * 使用合成复用原则
     */
    private ServerInitClose sic;
    private ServerDao sd;
    private  StudentModule studentModule;
    private ArrayList<Student> arrayList =new ArrayList<Student>();
    private ArrayList<Questions> arrayList2 =new ArrayList<Questions>();
    private ExamMD examMD;
    /**
     * 通过构造方法实现成员变量的初始化
     * @param sic
     */
    public ServerView(ServerInitClose sic, ServerDao sd, StudentModule studentModule,ExamMD examMD) {
        this.sic = sic;
        this.sd = sd;
        this.studentModule=studentModule;
        this. examMD=examMD;
    }

    /**
     * 自定义成员方法实现客户端发来消息的接收并处理
     */
    public void serverReceive() throws IOException, ClassNotFoundException {
          //读取硬盘中学员的信息
        studentModule.read(arrayList);
       examMD.readExam(arrayList2);
        System.out.println("学生信息为"+arrayList);
        System.out.println("考题信息为"+arrayList2);;
        while (true) {
            Object o =  sic.getOis().readObject();
            if (o instanceof StudentMessage){
                System.out.println("欢迎学员！");
                StudentMessage s=(StudentMessage) o;
                while (true){
                    if ( sd.serverStudentCheck(s,arrayList)){
                        s.setType("success");
                        System.out.println("success");
                        sic.getOos().writeObject(s);
                        break;
                    }else {
                        s.setType("fail");
                        // 将校验结果发送给客户端
                        sic.getOos().writeObject(s);
                        System.out.println("fail");
                    }
                }
                while (true){
                    char c=  sic.getOis().readChar();
                    System.out.println("--------------"+c);
                    if (c=='A'){
                               while (true){
                                   System.out.println("选择用户模块");
                                   char c1= (char)sic.getOis().readChar();
                                   System.out.println(c1);
                                   if (c1=='a'){
                                       System.out.println("修改密码");
                                       //接收客户端
                                       Student student= (Student)sic.getOis().readObject();
                                       studentModule.updatePWD(student,arrayList);
                                       System.out.println("修改成功"+arrayList);
                                   }else {
                                       System.out.println("退出用户模块");
                                       break;
                                   }
                               }
                           }
                    else if (c=='B'){
                        System.out.println("选择考试模块");
                            while (true){
                                char c2= (char)sic.getOis().readChar();
                                System.out.println(c2);
                                if (c2=='y'){
                                    System.out.println("选择开始考试");
                                    sic.getOos().reset();
                                    sic.getOos().writeObject(arrayList2);
                                    sic.getOos().writeObject(o);
                                    System.out.println((StudentMessage)o);
                                    System.out.println(arrayList2);
                                }else if (c2=='o'){
                                    System.out.println("选择导出");
                                }else if (c2=='p'){
                                    System.out.println("选择查询");
                                    sic.getOos().reset();


                                }else {
                                    System.out.println("退出考试模块");
                                    break;
                                }
                            }
                        }
                    else {
                        System.out.println("退出学员系统");
                            break;
                    }
                }
            }
            else if (o instanceof UserMessage){
                System.out.println("欢迎管理员！");
                UserMessage tum=(UserMessage)o;
                //调用方法实现管理员账号和密码信息的校验
                while (true) {
                    if (sd.serverManagerCheck(tum.getUser())) {
                        tum.setType("success");
                        System.out.println("success");
                        sic.getOos().writeObject(tum);
                        break;
                    } else if (!sd.serverManagerCheck(tum.getUser())){
                        tum.setType("fail");
                        // 将校验结果发送给客户端
                        sic.getOos().writeObject(tum);
                        System.out.println("fail");
                    }
                }

                System.out.println("服务器发送校验结果成功！");

                while (true) {
                    char c=  sic.getOis().readChar();
                    System.out.println("--------------"+c);
                    if (c=='S'){
                        while (true) {
                            System.out.println("你选择了学员管理模块");
                            char c1= (char)sic.getOis().readChar();
                            if (c1=='a'){
                                System.out.println("增加操作");
                                //接收客户端
                                Student student= (Student)sic.getOis().readObject();
                                studentModule.add(student,arrayList);
                                System.out.println("添加成功"+arrayList);
                            }else if (c1=='b'){
                                System.out.println("删除操作");
                                //接收客户端
                                Student student= (Student)sic.getOis().readObject();
                                studentModule.delete(student,arrayList);
                                System.out.println("删除成功"+arrayList);
                            }else if (c1=='c'){
                                System.out.println("修改操作");
                                //接收客户端
                                Student student= (Student)sic.getOis().readObject();
                                studentModule.update(student,arrayList);
                                System.out.println("修改成功"+arrayList);
                            }else if (c1=='d'){
                                System.out.println("查询操作");
                                //接收客户端
                                Student student= (Student)sic.getOis().readObject();
                                 Student result= studentModule.findOF(student,arrayList);
                                System.out.println("查询成功"+arrayList);
                                //返回客户端
                                sic.getOos().writeObject(result);
                            }else if (c1=='e'){
                                System.out.println("查询全部信息操作");
                                System.out.println(arrayList);
                                sic.getOos().reset();
                                sic.getOos().writeObject(arrayList);
                            }
                            else {
                                System.out.println("退出学员管理模块");

                                break;
                            }
                        }

                    }else  if (c=='E'){
                       while (true){
                            System.out.println("你选择了考题管理模块");
                            char c1= sic.getOis().readChar();
                           System.out.println(c1);
                            if (c1=='a'){
                                System.out.println("增加操作");
                                //接收客户端
                              Questions questions= (Questions)sic.getOis().readObject();
                              examMD.add(questions,arrayList2);
                                System.out.println("添加成功"+arrayList2);
                            }else if (c1=='b'){
                                System.out.println("删除操作");
                                //接收客户端
                                Questions questions= (Questions)sic.getOis().readObject();
                                examMD.delete(questions,arrayList2);
                                System.out.println("删除成功"+arrayList2);
                            }else if (c1=='c'){
                                System.out.println("修改操作");
                                //接收客户端
                                Questions questions= (Questions)sic.getOis().readObject();
                                examMD.update(questions,arrayList2);
                                System.out.println("修改成功"+arrayList2);
                            }else if (c1=='d'){
                                System.out.println("查询操作");
                                //接收客户端
                                Questions questions= (Questions)sic.getOis().readObject();
                              Questions result=  examMD.findOF(questions,arrayList2);
                                System.out.println("查询成功"+arrayList);
                                //返回客户端
                                sic.getOos().writeObject(result);
                            }else if (c1=='e'){
                                System.out.println("查询全部信息操作");
                                System.out.println(arrayList2);
                                sic.getOos().reset();
                                //返回客户端
                                sic.getOos().writeObject(arrayList2);
                            }
                            else {
                                System.out.println("退出考题管理模块");

                                break;
                            }
                        }
                    }else {
                        System.out.println("退出管理员系统");
                                break;
                    }
                }
            }
            else {
                System.out.println("退出在线考试系统");
                studentModule.save(arrayList);
                examMD.saveExam(arrayList2);
                System.out.println("数据已经保存在硬盘中--------");
                System.out.println("------------------------------------");
                System.out.println("------------------------------------");
                System.out.println("------------------------------------");
                System.out.println("------------------------------------");
                System.out.println("------------------------------------");
            }
        }


    }

}
